﻿using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
namespace MyCode.Tools
{
    public class Md5
    {
        public static string md5(string str, int code)
        {
            string strEncrypt = string.Empty;
            if (code == 16)
            {
                using (MD5 md5Hash = MD5.Create())
                {
                    byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(str));
                    //转换成字符串，并取9到25位
                    string sBuilder = BitConverter.ToString(data, 4, 8);
                    //BitConverter转换出来的字符串会在每个字符中间产生一个分隔符，需要去除掉
                    sBuilder = sBuilder.Replace("-", "");
                    return sBuilder.ToString().ToUpper();
                }

            }
            if (code == 32)
            {
                using (MD5 md5Hash = MD5.Create())
                {
                    byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(str));
                    StringBuilder sBuilder = new StringBuilder();
                    for (int i = 0; i < data.Length; i++)
                    {
                        sBuilder.Append(data[i].ToString("x2"));
                    }

                    string hash = sBuilder.ToString();
                    return hash.ToUpper();
                }
            }

            return strEncrypt;
        }
    }
}
